

using System;

using System.Collections;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.IO;
using System.Xml;
using JTC.MiniCommerce.Core.Serialization;

namespace JTC.MiniCommerce.Core {

  public class ExtendedProperties : Hashtable, IXmlSerializable {

    #region Methods
    
    #region Public

    /// <summary>
    /// New from XML.
    /// </summary>
    /// <param name="xml">The XML.</param>
    /// <returns></returns>
    public object NewFromXml(string xml) {
      Serializer serializer = new Serializer();
      return serializer.DeserializeObject(xml, typeof(ExtendedProperties).AssemblyQualifiedName);
    }

    /// <summary>
    /// To XML.
    /// </summary>
    /// <returns></returns>
    public string ToXml() {
      Serializer serializer = new Serializer();
      return serializer.SerializeObject(this, typeof(ExtendedProperties));
    }

    # region IXmlSerializable

    /// <summary>
    /// This property is reserved, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"></see> to the class instead.
    /// </summary>
    /// <returns>
    /// An <see cref="T:System.Xml.Schema.XmlSchema"></see> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"></see> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"></see> method.
    /// </returns>
    XmlSchema IXmlSerializable.GetSchema() {
      return null;
    }

    /// <summary>
    /// Generates an object from its XML representation.
    /// </summary>
    /// <param name="reader">The <see cref="T:System.Xml.XmlReader"></see> stream from which the object is deserialized.</param>
    void IXmlSerializable.ReadXml(XmlReader reader) {
      reader.Read();
      reader.ReadStartElement("dictionary");
      while(reader.NodeType != XmlNodeType.EndElement) {
        reader.ReadStartElement("item");
        string key = reader.ReadElementString("key");
        string value = reader.ReadElementString("value");
        reader.ReadEndElement();
        reader.MoveToContent();
        this.Add(key, value);
      }
      reader.ReadEndElement();
    }

    /// <summary>
    /// Converts an object into its XML representation.
    /// </summary>
    /// <param name="writer">The <see cref="T:System.Xml.XmlWriter"></see> stream to which the object is serialized.</param>
    void IXmlSerializable.WriteXml(XmlWriter writer) {
      writer.WriteStartElement("dictionary");
      foreach(object key in this.Keys) {
        object value = this[key];
        writer.WriteStartElement("item");
        writer.WriteElementString("key", key.ToString());
        writer.WriteElementString("value", value.ToString());
        writer.WriteEndElement();
      }
      writer.WriteEndElement();
    }
    
    #endregion
    
    #endregion
    
    #endregion
    
  }
}
